﻿using System;
using System.Windows.Forms;

namespace sy1_8
{
    public partial class Form1 : Form
    {
        private int remainingSeconds; // 剩余秒数

        public Form1()
        {
            InitializeComponent();
            UpdateTimeDisplay(0); // 初始显示00:00
        }

        // 计时按钮
        private void btnStart_Click(object sender, EventArgs e)
        {
            // 验证输入
            if (int.TryParse(txtSeconds.Text, out int seconds) && seconds > 0)
            {
                remainingSeconds = seconds;
                UpdateTimeDisplay(remainingSeconds);
                timer1.Enabled = true; // 启动计时器
                btnStart.Enabled = false; // 禁用计时按钮
                txtSeconds.Enabled = false; // 禁用输入框
            }
            else
            {
                MessageBox.Show("请输入有效的正整数秒数！", "输入错误");
            }
        }

        // 停止按钮
        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false; // 停止计时器
            btnStart.Enabled = true;
            txtSeconds.Enabled = true;
            UpdateTimeDisplay(0);
        }

        // 计时器事件（每秒触发）
        private void timer1_Tick(object sender, EventArgs e)
        {
            remainingSeconds--;
            UpdateTimeDisplay(remainingSeconds);

            // 计时结束
            if (remainingSeconds <= 0)
            {
                timer1.Enabled = false;
                btnStart.Enabled = true;
                txtSeconds.Enabled = true;
                MessageBox.Show("计时结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        // 更新时间显示（格式化为MM:SS）
        private void UpdateTimeDisplay(int totalSeconds)
        {
            int minutes = totalSeconds / 60;
            int seconds = totalSeconds % 60;
            lblTime.Text = $"{minutes:D2}:{seconds:D2}"; // 两位数显示
        }
    }
}